use super::*;

#[inline(always)]
fn step(
    i:usize, k:u32,
    a:&mut [u32], b:&mut [u32],
    c:&mut [u32], d:&mut [u32],
    e:&mut [u32], f:&mut [u32],
    g:&mut [u32], h:&mut [u32],
    t1:&mut [u32], t2:&mut [u32],
    w: &mut [[u32;ISAL_HASH_SEGS];ISAL_SHA256_BLOCK_SIZE/std::mem::size_of::<u32>()], 
    data:&[u8],
) {
    macro_rules! S0 { ($w:expr) => { $w.rotate_right(7) ^ $w.rotate_right(18) ^ ($w >> 3)};}
    macro_rules! S1 { ($w:expr) => { $w.rotate_right(17) ^ $w.rotate_right(19) ^ ($w >> 10)};}
    macro_rules! s0 { ($a:expr) => { $a.rotate_right(2) ^ $a.rotate_right(13) ^ $a.rotate_right(22) };}
    macro_rules! s1 { ($e:expr) => { $e.rotate_right(6) ^ $e.rotate_right(11) ^ $e.rotate_right(25) };}
    macro_rules! maj { ($a:expr, $b:expr, $c:expr) => { ($a & $b) ^ ($a & $c) ^ ($b & $c)};}
    macro_rules! ch { ($e:expr, $f:expr, $g:expr) => { ($e & $f) ^ ($g & !$e) };}
    macro_rules! Ws { ($x:expr, $s:expr) => { w[($x) & 15][$s] }; }
    macro_rules! store_w { ($s:expr, $i:expr, $w:expr, $data:expr) => { 
        $w[$i][$s] = u32::from_be_bytes($data[($i * ISAL_HASH_SEGS + $s)*std::mem::size_of::<u32>()..($i * ISAL_HASH_SEGS + $s + 1)*std::mem::size_of::<u32>()].try_into().unwrap()); 
    }; }
    macro_rules! update_w { 
        ($s:expr, $i:expr, $w:expr) => { 
            Ws!($i, $s) = Ws!($i - 16, $s)
                            .wrapping_add(S0!(&Ws!($i - 15, $s)))
                            .wrapping_add(Ws!($i - 7, $s))
                            .wrapping_add(S1!(&Ws!($i - 2, $s)));
        };
    }
    macro_rules! update_t2 { ($s:expr, $a:expr, $b:expr, $c:expr) => { t2[$s] = s0!($a[$s]).wrapping_add(maj!($a[$s], $b[$s], $c[$s])); }; }
    macro_rules! update_t1 {
        ($s:expr, $h:expr, $e:expr, $f:expr, $g:expr, $i:expr, $k:expr) => {
            t1[$s] = $h[$s]
                        .wrapping_add(s1!($e[$s]))
                        .wrapping_add(ch!($e[$s], $f[$s], $g[$s]))
                        .wrapping_add($k)
                        .wrapping_add(Ws!($i, $s));
        };
    }
    macro_rules! update_d { ($s:expr, $d:expr, $t1:expr) => { $d[$s] = $d[$s].wrapping_add($t1[$s]); }; }
    macro_rules! update_h { ($s:expr, $h:expr, $t1:expr, $t2:expr) => { $h[$s] = $t1[$s].wrapping_add($t2[$s]); }; }

    macro_rules! store_16w { ($i:expr, $w:expr, $data:expr) => { seq!(I in 0..16 { store_w!(I, $i, $w, $data); }); }; }
    macro_rules! update_16w { ($i:expr, $w:expr) => { seq!(I in 0..16 { update_w!(I, $i, $w); }); }; }
    macro_rules! update_16t2 { ($a:expr, $b:expr, $c:expr) => { seq!(I in 0..16 { update_t2!(I, $a, $b, $c); }); } }
    macro_rules! update_16t1 { ($h:expr, $e:expr, $f:expr, $g:expr, $i:expr, $k:expr) => { seq!(I in 0..16 { update_t1!(I, $h, $e, $f, $g, $i, $k); }); } }
    macro_rules! update_16d { ($d:expr, $t1:expr) => { seq!(I in 0..16 { update_d!(I, $d, $t1); }); } }
    macro_rules! update_16h { ($h:expr, $t1:expr, $t2:expr) => { seq!(I in 0..16 { update_h!(I, $h, $t1, $t2); }); } }

    if i < 16 {
            store_16w!(i, w, data);
    } else {
            update_16w!(i, w);
    }
    update_16t2!(a, b, c);
    update_16t1!(h, e, f, g, i, k);
    update_16d!(d, t1);
    update_16h!(h, t1, t2);
}

#[inline(always)]
fn get_abcdefgh(n:usize, digests:&[[u32;ISAL_HASH_SEGS];ISAL_SHA256_DIGEST_WORDS]) -> [u32;ISAL_HASH_SEGS] {
    digests[n]
}

#[inline(always)]
fn add_abcdefgh(xx:&mut [u32], n:usize, digests:&mut [[u32;ISAL_HASH_SEGS];ISAL_SHA256_DIGEST_WORDS]) {
    seq!(I in 0..16 { digests[n][I] = digests[n][I].wrapping_add(xx[I]); });
}

fn mh_sha256_single(
    input:&[u8], 
    digests:&mut [[u32;ISAL_HASH_SEGS];ISAL_SHA256_DIGEST_WORDS], 
    frame_buffer:&mut [[u32;ISAL_HASH_SEGS];ISAL_SHA256_BLOCK_SIZE/std::mem::size_of::<u32>()],
) {
    let mut aa = get_abcdefgh(0, digests);
    let mut bb = get_abcdefgh(1, digests);
    let mut cc = get_abcdefgh(2, digests);
    let mut dd = get_abcdefgh(3, digests);
    let mut ee = get_abcdefgh(4, digests);
    let mut ff = get_abcdefgh(5, digests);
    let mut gg = get_abcdefgh(6, digests);
    let mut hh = get_abcdefgh(7, digests);
    
    let mut t1 = [0u32;ISAL_HASH_SEGS];
    let mut t2 = [0u32;ISAL_HASH_SEGS];

    const K:[u32;64] = [   
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
        0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
        0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
        0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
        0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
        0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
        0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
        0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
        0xc67178f2
    ];

    let w = frame_buffer;
    let data = input;

    seq!(I in 0..8 {        
        step(I * 8, K[I * 8], &mut aa, &mut bb, &mut cc, &mut dd, &mut ee, &mut ff, &mut gg, &mut hh, &mut t1, &mut t2, w, data);
        step(I * 8 + 1, K[I * 8 + 1], &mut hh, &mut aa, &mut bb, &mut cc, &mut dd, &mut ee, &mut ff, &mut gg, &mut t1, &mut t2, w, data);
        step(I * 8 + 2, K[I * 8 + 2], &mut gg, &mut hh, &mut aa, &mut bb, &mut cc, &mut dd, &mut ee, &mut ff, &mut t1, &mut t2, w, data);
        step(I * 8 + 3, K[I * 8 + 3], &mut ff, &mut gg, &mut hh, &mut aa, &mut bb, &mut cc, &mut dd, &mut ee, &mut t1, &mut t2, w, data);
        step(I * 8 + 4, K[I * 8 + 4], &mut ee, &mut ff, &mut gg, &mut hh, &mut aa, &mut bb, &mut cc, &mut dd, &mut t1, &mut t2, w, data);
        step(I * 8 + 5, K[I * 8 + 5], &mut dd, &mut ee, &mut ff, &mut gg, &mut hh, &mut aa, &mut bb, &mut cc, &mut t1, &mut t2, w, data);
        step(I * 8 + 6, K[I * 8 + 6], &mut cc, &mut dd, &mut ee, &mut ff, &mut gg, &mut hh, &mut aa, &mut bb, &mut t1, &mut t2, w, data);
        step(I * 8 + 7, K[I * 8 + 7], &mut bb, &mut cc, &mut dd, &mut ee, &mut ff, &mut gg, &mut hh, &mut aa, &mut t1, &mut t2, w, data);
    });


    add_abcdefgh(&mut aa, 0, digests);
    add_abcdefgh(&mut bb, 1, digests);
    add_abcdefgh(&mut cc, 2, digests);
    add_abcdefgh(&mut dd, 3, digests);
    add_abcdefgh(&mut ee, 4, digests);
    add_abcdefgh(&mut ff, 5, digests);
    add_abcdefgh(&mut gg, 6, digests);
    add_abcdefgh(&mut hh, 7, digests);
}

#[repr(C, align(64))]
#[derive(Clone, Debug, Default)]
pub struct MhSha256BaseWordFrameBuffer([[u32;ISAL_HASH_SEGS];ISAL_SHA256_BLOCK_SIZE/std::mem::size_of::<u32>()]);

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)]
pub struct MhSha256Base;

impl MhSha256BlockFunction for MhSha256Base {
    type FrameBuffer = MhSha256BaseWordFrameBuffer;
    fn mh_sha256_block_function(input_data: &[u8], mh_sha256_segs_digests: &mut [[u32;ISAL_HASH_SEGS];ISAL_SHA256_DIGEST_WORDS], aligned_frame_buffer: &mut Self::FrameBuffer, num_blocks: u32) {
        let mut input_data = input_data;
        for _ in 0..num_blocks {
                mh_sha256_single(input_data, mh_sha256_segs_digests, &mut aligned_frame_buffer.0);
                input_data = &input_data[ISAL_MH_SHA256_BLOCK_SIZE..];
        }
        return;
    }
}
